package com.markus.code.exam.qushikeji;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * Author:markusZhang
 * Date:Create in 2020/9/12 20:05
 * todo:
 */
public class Main2 {
    public static class TreeNode{
        char val;
        TreeNode left;
        TreeNode right;
        public TreeNode(char val){
            this.val = val;
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            String inStr = scanner.next();
            String aftStr = scanner.next();
            char []inorder = new char[inStr.length()];
            char []aftorder = new char[aftStr.length()];
            for (int i = 0; i < inorder.length; i++) {
                inorder[i] = inStr.charAt(i);
                aftorder[i] = aftStr.charAt(i);
            }
            Main2 demo = new Main2();
            TreeNode root = demo.buildTree(inorder,aftorder);
            demo.printTree(root);
        }
    }
    private TreeNode buildTree(char []inorder,char[] postorder){
        if (inorder.length == 0 || postorder.length != inorder.length){
            return null;
        }
        Map<Character,Integer> map = new HashMap<>();
        for (int i=0;i<inorder.length;i++){
            map.put(inorder[i],i);
        }
        return buildNode(inorder,0,inorder.length-1,postorder,0,postorder.length-1,map);
    }
    private TreeNode buildNode(char[] inorder,int begin1,int end1,char[] postorder,int begin2,int end2,Map<Character,Integer> map){
        if (begin2 > end2){
            return null;
        }
        if (begin1 == end1){
            return new TreeNode(inorder[begin1]);
        }
        TreeNode root = new TreeNode(postorder[end2]);
        int index = map.get(postorder[end2]);
        root.left = buildNode(inorder,begin1,index-1,postorder,begin2,begin2+index-begin1-1,map);
        root.right = buildNode(inorder,index+1,end1,postorder,begin2+index-begin1,end2-1,map);
        return root;
    }
    private void printTree(TreeNode root){
        if (root != null){
            System.out.print(root.val);
            printTree(root.left);
            printTree(root.right);
        }
    }
}
